package com.plian.system.controller.pm.annualdonationexecute;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.plian.system.entity.pm.annualdonationexecute.AnnualDonationExecute;
import com.plian.system.entity.pm.annualdonationexecutemain.AnnualDonationExecuteMain;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.mybatis.search.SearchEntity;
import com.plian.system.mybatis.search.SearchParamEntiy;
import com.plian.system.mybatis.search.TokenSearch;
import com.plian.system.mybatis.support.Condition;
import com.plian.system.mybatis.support.Query;
import com.plian.system.service.pm.annualdonationexecute.IAnnualDonationExecuteService;
import com.plian.system.service.pm.annualdonationexecutemain.IAnnualDonationExecuteMainService;
import com.plian.system.vo.pm.annualdonationexecute.AnnualDonationExecuteVO;
import com.plian.system.wrapper.pm.annualdonationexecute.AnnualDonationExecuteWrapper;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 企业年度捐赠计划执行明细 控制器
 *
 * @author panqb
 * @since 2022-04-20
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/api/annuldonationexecute")
@Api(value = "企业年度捐赠计划执行明细", tags = "企业年度捐赠计划执行明细")
public class AnnualDonationExecuteController {

    IAnnualDonationExecuteMainService annualDonationExecuteMainService;

    IAnnualDonationExecuteService annualDonationExecuteService;

    AnnualDonationExecuteWrapper annualDonationExecuteWrapper;

    /**
     * 年度捐赠计划项目执行明细报表
     * @param query
     * @return
     */
    @PostMapping("report")
    public ListResult<AnnualDonationExecuteVO> report(@RequestBody Query query){
        ListResult<AnnualDonationExecuteVO> listResult=new ListResult<>(CommonCode.SUCCESS);
        List<SearchEntity> entityList = query.getSearch();
        if (query.getAscs()==null&&query.getDescs()==null){
            query.setDescs("create_time");
        }
        QueryWrapper<AnnualDonationExecuteMain> queryWrapper = SearchParamEntiy.getQueryWrapper(
                Condition.getQueryWrapper(new AnnualDonationExecuteMain()), entityList
        );
        TokenSearch<AnnualDonationExecuteMain> tokenSearch = new TokenSearch<>();
		tokenSearch.addTokenQuery(queryWrapper, query);
        List<AnnualDonationExecuteMain> list=annualDonationExecuteMainService.list(queryWrapper);
        if(list.size()==0){
            return listResult;
        }
        List<Long> mainIds=list.stream().map(AnnualDonationExecuteMain::getId).collect(Collectors.toList());
        Map<Long,String> yearMap=list.stream().collect(Collectors.toMap(AnnualDonationExecuteMain::getId,AnnualDonationExecuteMain::getYear ));
        QueryWrapper<AnnualDonationExecute> recordQueryWrapper=new QueryWrapper<>();
        recordQueryWrapper.in("parent_id",mainIds);
        List<AnnualDonationExecute> recordList=annualDonationExecuteService.list(recordQueryWrapper);
        List<AnnualDonationExecuteVO> recordVOList=annualDonationExecuteWrapper.entityToVO(recordList);
        recordVOList.forEach(record->{
            Long parentId=record.getParentId();
            if(yearMap.containsKey(parentId)){
                record.setYear(yearMap.get(parentId));
            }
        });
        listResult.setList(recordVOList);
        listResult.setTotal(recordVOList.size());
        return listResult;
    }
}
